library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble  3.1.4     ✓ dplyr   1.0.7
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   2.0.1     ✓ forcats 0.5.1
## ✓ purrr   0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks plotly::filter(), stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gt)
library(htmltools)


# Esto es para cada una de las categorias:

iris %>% 
  filter(Species == "setosa") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

iris %>% 
  filter(Species == "virginica") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

iris %>% 
  filter(Species == "versicolor") %>% 
  ggplot() +
  geom_point(aes(x = Sepal.Width,y=Sepal.Length))

# En su lugar podemos un split y utilizando un map:

# Split separa un dataframe en las categorías que tiene la vairable seleccionada
# base %>% split(.$variable)
iris_plot_lista <- iris %>% 
  split(.$Species) %>% 
  map(~.x %>% 
        ggplot() +
        geom_point(aes(x = Sepal.Width,y=Sepal.Length)))

names(iris_plot_lista)
## [1] "setosa"     "versicolor" "virginica"
# Imprimir cada elemento de la lista, se imprime un gráfico

iris_plot_lista[[1]]

iris_plot_lista[[2]]

iris_plot_lista[[3]]

# Anidar la información en una columna y siempre trabajar dentro de un data.frame:

nest_iris <- iris %>% 
  group_by(Species) %>% 
  nest_by()

# Añadió al anterior dataframe anidado una columna con las imágenes creadas

nest_iris_2 <- nest_iris %>% 
  mutate(plot_point = list(ggplot(data ) +
                             geom_point(aes(x = Sepal.Width,y=Sepal.Length)))
  )

# ploty::ggplotly (grafico creado previamente)
# Hace que aparezce la imagen con opciones de edición

plotly::ggplotly(iris_plot_lista[[1]])
nest_iris_2 <- nest_iris_2 %>% 
  mutate(plotly_list = list(plotly::ggplotly(plot_point)))


# Transforma la columna ploty_list de elemento plotly a elemento html

nest_iris_2 <- nest_iris_2 %>% 
  mutate(plotly_list = list(plotly_list %>%
           as.tags() %>%
           as.character() %>%
           htmltools::HTML()))

# Como ya se usaron los elementos de la tabla anidada, en este se deja solo lo que interesa
# las categorías y su respectiva imagen en html

nest_iris_3 <- nest_iris_2 %>% 
  select(-data,-plot_point) 


# Se imprime como si fuese una página web, sólo los elementos que quedaron
  
  DT::datatable(nest_iris_3, escape = FALSE,options = list(
  fnDrawCallback = htmlwidgets::JS(
    '
function(){
  HTMLWidgets.staticRender();
}
'
  )))

Termometro de la tendencia politica:


                                
$0-1   1-2   2-3   3-4   4-5   5-6   6-7   7-8   8-9   9-10 

enlaces <- c(
"https://thumbs.dreamstime.com/z/flor-del-iris-setosa-del-iris-67468042.jpg",

"https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/Iris_virginica.jpg/250px-Iris_virginica.jpg",

"https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.consejosparamihuerto.com%2Ffamilias%2Firis-versicolore%2F&psig=AOvVaw0k6NQUBUn92QtLkqLrRd27&ust=1636636896960000&source=images&cd=vfe&ved=0CAsQjRxqFwoTCLjx1NfxjfQCFQAAAAAdAAAAABAD")


nest_iris_4 <- nest_iris_3 %>% 
  mutate(
    src = list(enlaces),
    Species = list(str_c('<div><b>',Species,'</b><br><img src="',src,'">',
                         "</div>"))) %>% 
  select(-src)


  DT::datatable(nest_iris_4, escape = FALSE,options = list(
  fnDrawCallback = htmlwidgets::JS(
    '
function(){
  HTMLWidgets.staticRender();
}
'
  )))